<?php

namespace App\Store\Models\Sysset\Commission;

use App\Common\Services\Common;
use App\Common\Services\Store\Common as StoreCommon;
use App\Store\Models\Account\SiteStore;
use App\Store\Models\Basic\GoodsCategory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Http\Request;

class Recovery extends Model
{
    protected $table = 'site_sysset_commission_recovery';
    public $timestamps = false;
    protected $primaryKey = 'id';
    protected $guarded = ['_token'];

    /**
     *获取列表
     */
    public static function getList(Request $request)
    {
        $accountinfo = StoreCommon::getCurrentAccountinfo($request);
        $cp_id = $accountinfo['cp_id'];

        $query = self::with(['storeinfo', 'goods_category'])->where('cp_id',$cp_id)->where('is_del', 1);

        //目标门店
        $store_id = $request['store_id'] ?? 0;
        if ($store_id > 0) {
            $query->where('store_id', $store_id);
        }

        //商品类别
        $goods_category_id = $request['goods_category_id'] ?? 0;
        if ($goods_category_id > 0) {
            $query->where('goods_category_id', $goods_category_id);
        }

        //折扣区间
        $discount_min = $request->discount_min;
        if ($discount_min != '') {
            $query->where('discount_min', '>=', $discount_min);
            $query->where('discount_max', '<=', $discount_min);
        }
        $discount_max = $request->discount_max;
        if ($discount_max != '') {
            $query->where('discount_max', '<=', $discount_max);
            $query->where('discount_min', '>=', $discount_max);
        }

        $query->orderBy('create_at', 'desc');

        $isgetall = $request['isgetall'] ?? 0;
        if ($isgetall == 0) {
            $limit = $request->limit ?? 10;
            $list = $query->paginate($limit)->toArray();
            return Common::paginateDataToArray($list);
        } else {
            $list = $query->get()->toArray();
            return ['list' => $list];
        }
    }

    public function storeinfo()
    {
        return $this->hasOne(SiteStore::class, 'id', 'store_id')->select('id', 'store_name');
    }

    public function goods_category()
    {
        return $this->hasOne(GoodsCategory::class, 'id', 'goods_category_id')->select('id', 'category_name');
    }
}

